<template>
  <div class="table-box">
    <ProTable ref="proTable" title="角色列表" :columns="columns" :request-api="getRoleList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增')"> 新增角色 </el-button>
      </template>
      <!-- Expand -->
      <template #expand="scope">
        {{ scope.row }}
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-dropdown trigger="hover">
          <el-button type="primary">
            操作菜单
            <el-icon><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>
                <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)">
                  编辑
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteAccount(scope.row)">
                  删除
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-auth="'set'" type="primary" link :icon="View" @click="openPowerDialog(scope.row.id)">
                  权限分配
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item :icon="Check">
                <el-button v-auth="'posSet'" type="primary" link :icon="Setting" @click="openRolePosDialog(scope.row.id)">
                  收银权限
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </template>
    </ProTable>
    <RoleDialog ref="dialogRef" />
    <PowerDialog ref="powerDialogRef" />
    <RolePosDialog ref="rolePosDialogRef" />
  </div>
</template>

<script setup lang="tsx" name="roleManager">
import { ref } from "vue";
import { System } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import RoleDialog from "@/views/system/components/RoleDialog.vue";
import PowerDialog from "@/views/system/components/PowerDialog.vue";
import RolePosDialog from "@/views/system/components/RolePosDialog.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { CirclePlus, EditPen, View, Delete, Setting } from "@element-plus/icons-vue";
import {
  getRoleList,
  createRole,
  editRole,
  deleteRole,
  setMenuPageDict,
  setSysRolePos,
  getSysRolePos
} from "@/api/modules/system";

const proTable = ref<ProTableInstance>();

// 表格配置项
const columns: ColumnProps<System.ResRoleList>[] = [
  { type: "index", width: 60 },
  { sortable: true, prop: "roleName", label: "角色名称", minWidth: 240, align: "left", search: { el: "input" } },
  { sortable: true, prop: "roleCode", label: "角色标识", minWidth: 120, align: "left", search: { el: "input" } },
  { sortable: true, prop: "comments", minWidth: 160, label: "备注" },
  { sortable: true, prop: "createTime", label: "创建时间", minWidth: 164, align: "left" },
  { sortable: true, prop: "updateTime", label: "修改时间", minWidth: 164, align: "left" },
  { prop: "operation", label: "操作", fixed: "right", width: 140 }
];

// 删除
const deleteAccount = async (params: System.ResRoleList) => {
  await useHandleData(deleteRole, { id: params.id }, `删除【${params.roleName}】角色`);
  proTable.value?.getTableList();
};

// 权限管理
const powerDialogRef = ref<InstanceType<typeof PowerDialog> | null>(null);
const openPowerDialog = (rowId: number) => {
  const params = {
    title: "选择权限",
    isView: false,
    id: rowId,
    api: setMenuPageDict,
    getTableList: proTable.value?.getTableList
  };
  powerDialogRef.value?.acceptParams(params);
};

// 打开 drawer(新增、查看、编辑)
const dialogRef = ref<InstanceType<typeof RoleDialog> | null>(null);
const openDialog = (title: string, row: Partial<System.ResRoleList> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: title === "新增" ? createRole : title === "编辑" ? editRole : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};

const rolePosDialogRef = ref<InstanceType<typeof RolePosDialog> | null>(null);
const openRolePosDialog = async (rowId: number) => {
  const { data } = await getSysRolePos({ id: rowId });
  console.log(data);

  const params = {
    title: "编辑收银权限",
    isView: false,
    row: { ...data },
    api: setSysRolePos
  };
  rolePosDialogRef.value?.acceptParams(params);
};
</script>
